<template>
  <div>
    <div>
      <FormPanel
        ref="formPanel"
        :list="formList"
        :cols="1"
        :formType="type"
        labelWidth="100"
        :isSubmitBtn="false"
        @formChange="formChangeHandle"
      />
    </div>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'center'
      }"
    >
      <el-button @click="closeDialog">取 消</el-button>
      <AjaxButton :click="saveDialog" type="primary">保 存</AjaxButton>
    </div>
  </div>
</template>

<script>
import {
  getFailModelsById,
  addFailModel,
  ModifyFailModels
 } from '@/api/potenCus/basedata/failModel';
import {
  getBrandList,
  getSeriesList,
  getModelList,
  getPackageList
 } from '@/api/potenCus/basedata/searchTestDriveCars';
import { dictionary } from '@/utils/dictMixin';

export default {
  name: 'failModel',
  mixins: [dictionary],
  props: {
    type: {
      type: String,
      default: 'add'
    },
    rowData: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    this.saveData = null;
    return {
      formList: this.createFormList()
    };
  },
  created() {
    this.init();
    this.getDictBrandList()
  },
  methods: {
    init() {
      if (this.type === 'edit') {
        // 更新接口需要的记录 ID
        this.recordId = this.rowData.failModelId;
        this.formList.forEach(x => {
          x.initialValue = this.rowData[x.fieldName];
        });
      }
    },
    createFormList() {
      return [
        {
          type: 'SELECT',
          label: '战败品牌名称',
          fieldName: 'intentBrand',
          placeholder: '请选择',
          style: { width: '200px' },
          itemList: [],
          rules: [{ required: true, message: '请选择战败品牌名称', trigger: 'blur' }],
          change: val => {
            this.changeBrandSelect(val);
          }
        },
        {
          type: 'SELECT',
          label: '战败车系名称',
          fieldName: 'intentSeries',
          placeholder: '请选择',
          style: { width: '200px' },
          itemList: [],
          rules: [{ required: true, message: '请选择战败车系名称', trigger: 'blur' }],
          change: val => {
            this.changeSeriesSelect(val);
          }
        },
        {
          type: 'INPUT',
          label: '战败车型代码',
          fieldName: 'failModel',
          placeholder: '请输入',
          style: { width: '200px' },
          rules: [{ required: true, message: '请填写战败车型代码', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '战败车型描述',
          fieldName: 'failModelDesc',
          placeholder: '请输入',
          style: { width: '300px' },
          rules: [{ required: true, message: '请填写战败车型描述', trigger: 'blur' }]
        },
        {
          type: 'SELECT',
          label: '是否有效',
          fieldName: 'isValid',
          placeholder: '请选择',
          style: { width: '200px' },
          initialValue: 10041001,
          itemList: this.createDictList('1004'),
          rules: [{ required: true, message: '请选择是否有效', trigger: 'blur' }]
        }
      ];
    },
    formChangeHandle(val) {
      this.saveData = val;
    },
    async saveDialog() {
      this.$refs.formPanel.SUBMIT_FORM();
      if (this.saveData !== null) {
        let res = {};
        if (this.type === 'add') {
          res = await addFailModel(this.saveData);

        }
        if (this.type === 'edit') {
          res = await ModifyFailModels(this.saveData, this.recordId);
        }
        if (res.resultCode === 200) {
          this.closeDialog('refresh');
          this.$message.success("保存成功!");
        }
      }
    },
    closeDialog(param) {
      this.$emit('close', false, param);
    },
    async getDictBrandList() {
      const res = await getBrandList();
      if (res.resultCode === 200) {
        this.formList[0].itemList = res.data.map(x => ({ value: x.brandId, text: x.brandName }));
        // this.topFilterList = [...this.topFilterList]
      }
    },
    async changeBrandSelect(brandsid) {
      if (!brandsid) {
        this.formList[1].initialValue = '';
        this.formList[1].itemList = [];
        this.formList[2].initialValue = '';
        this.formList[2].itemList = [];
        this.formList[3].initialValue = '';
        this.formList[3].itemList = [];
      } else {
        const res = await getSeriesList(brandsid);
        if (res.resultCode === 200) {
          this.formList[1].itemList = res.data.map(x => ({ value: x.seriesId, text: x.seriesName }));
        }
        if (this.formList[1].initialValue) {
          this.formList[1].initialValue = '';
        }

      }
    },
    async changeSeriesSelect(seriessid) {
      const brandsid = this.formList[0].initialValue;
      if (!brandsid || !seriessid) {
        this.formList[2].initialValue = '';
        this.formList[2].itemList = [];
        this.formList[3].initialValue = '';
        this.formList[3].itemList = [];
      } else {
        const res = await getModelList(brandsid, seriessid);
        if (res.resultCode === 200) {
          this.formList[2].itemList = res.data.map(x => ({ value: x.modelId, text: x.modelName }));
        }
      }
    },

  }
};
</script>

<style lang="less" scoped>
</style>
